import {
	nanoid
} from 'nanoid/non-secure'
// const crypto = require('crypto');
// 失效时间：1天
const exp = 86400

function login2() {
	// 直接登录，刷新内存即可
	const app = getApp()
	let p2 = null
	uni.login({
		success: (coderes) => {
			/* let gender = null
			// 获取用户基础信息：头像，gender，nickName。静默获取，alipay获取不到
			uni.getUserInfo({
				provider: 'weixin',
				success(infoRes) {
					// 测试只有在微信端能拿到 支付宝端拿不到
					// 头像只能拿到未知头像,未知性别等，没意义：avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
					console.info('uniapp用户基础信息', infoRes)
					gender = infoRes.userInfo.gender
				}
			}); */
			// console.log(coderes); // 发送 res.code 到后台换取 openId, sessionKey, unionId
			uniCloud.callFunction({
				name: 'zhy-mp-weixin',
				data: {
					code: coderes.code,
					type: 1
				},
				success: (res) => {
					uni.setStorageSync(app.globalData.session.userKey,
						res.result,
						exp);
					// 查询数据库 并存储再storage中
					uniCloud.callFunction({
						name: 'zhy-business-user-info',
						data: {
							userKey: res.result,
							operateType: 1,
							login_plate: 'MP-WEIXIN',
							// 生成user_id
							generateKey: nanoid(10),
							generateName: '微信用户'
						},
						success: (userRes) => {
							// console.log('userRes:' + JSON.stringify(userRes));
							uni.setStorageSync(app
								.globalData.session
								.userKey, res
								.result, exp);
							uni.setStorageSync(app
								.globalData.session
								.userType,
								userRes.result.userType,
								exp);
							uni.setStorageSync(app
								.globalData.session
								.userAccountType,
								userRes.result
								.userType, exp);
							uni.setStorageSync(app
								.globalData.session
								.userInfo,
								userRes.result.userInfo,
								exp);
							if (userRes.result.userType ==
								2) {
								uni.setStorageSync(app
									.globalData
									.session
									.businessInfo,
									userRes.result
									.businessInfo,
									exp);
							}
						}
					})
				},
				fail: (res) => {
					console.log('云函数调用失败' + res)
				}
			})
		}
	})
	// return new Promise(p1).then(p1).then(p2)
}

// 顺序执行
function login() {
	return new Promise((resolve, reject) => {
		getWXCode().then((res) => {
			return getWXOpenid(res)
		}).then(res => {
			console.log('成功!!!' + JSON.stringify(res));
			resolve('wxlogin:true')
		}).catch(err => {
			console.log('失败!!!' + JSON.stringify(err));
		})
		console.log('.。。。。。。。。。。。。。。。');
	})
}

function getWXCode() {
	return new Promise((resolve, reject) => {
		uni.login({
			success: (coderes) => {
				console.log('coderes:' + JSON.stringify(coderes));
				resolve(coderes)
			},
			fail: (coderes) => {
				console.log('登录失败' + JSON.stringify(coderes))
				reject(coderes)
			}
		})
	})
}

function getWXOpenid(coderes) {
	const app = getApp()
	return new Promise((resolve, reject) => {
		uniCloud.callFunction({
			name: 'zhy-mp-weixin',
			data: {
				code: coderes.code,
				type: 1
			},
			success: (res) => {
				uni.setStorageSync(app.globalData.session.userKey, res.result, exp);
				// 查询数据库 并存储再storage中
				uniCloud.callFunction({
					name: 'zhy-business-user-info',
					data: {
						userKey: res.result,
						operateType: 1,
						login_plate: 'MP-WEIXIN',
						// 生成user_id
						generateKey: 'wx' + nanoid(10),
						generateName: '微信用户'
					},
					success: (userRes) => {
						// console.log('userRes:' + JSON.stringify(userRes));
						uni.setStorageSync(app.globalData.session.userKey, res.result,
							exp);
						uni.setStorageSync(app.globalData.session.userType, userRes
							.result.userType,
							exp);
						uni.setStorageSync(app.globalData.session.userAccountType,
							userRes.result
							.userType, exp);
						uni.setStorageSync(app.globalData.session.userInfo, userRes
							.result.userInfo,
							exp);
						if (userRes.result.userType == 2) {
							uni.setStorageSync(app
								.globalData
								.session
								.businessInfo,
								userRes.result
								.businessInfo,
								exp);
						}
						resolve(userRes)
					}
				})
			},
			fail: (res) => {
				console.log('云函数调用失败' + res)
				reject(res)
			}
		})
	})
}
module.exports = {
	login
}